var api = require("../../../api.js"),
    utils = require("../../../utils/utils.js"),
    app = getApp();

var countdown = 60;
var settime = function(that) {
    if (countdown == 0) {
        that.setData({
            is_show: true
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })
        countdown--;
    }
    setTimeout(function() {
        settime(that)
    }, 1000)
};

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 会员卡卡号
        user_num: '',
        // 持卡人电话
        user_tel: "",
        // 持卡人姓名
        user_name: "",
        // 验证码
        user_yzm: '',

        last_time: '',
        is_show: true
    },
    // to bind card
    tobind_card: function() {
        wx.redirectTo({
            url: '/page/health/pages/bind_card/bind_card',
        })
    },




    // 驗證登錄
    formSubmit: function() {
        var that = this;
        var code = that.data.code;
        console.log(code)
        console.log("================code=======================")
        var yzm = that.data.user_yzm;
        var user_num = that.data.user_num;
        // var user_name = that.data.user_name;
        var phone = that.data.user_tel;

        if (yzm == '' || yzm == ' ' || phone.length != 11) {
            console.log("部位空");
            wx.showToast({
                title: '填写有误',
                icon: 'loading',
                duration: 1500,
                mask: true,
            })
        }
        // 判断会员卡卡号状态
        app.request({
            url: api.form.login,
            data: {
                card: user_num,
                phone: phone,
                code: code
            },
            header: {
                'content-type': "application/x-www-form-urlencoded"
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
                console.log(res.data)
                // if (res.data.code == 1) {
                //     wx.showToast({
                //         title: res.data.msg,
                //         duration: 1500,
                //         mask: true,
                //     })
                // }
                if (code === yzm & yzm != '' & phone != '') {
                    if (res.code == 0) {
                        wx.redirectTo({
                            url: '/page/health/pages/archives/archives?card=' + user_num,
                            success: function(res) {},
                            fail: function(res) {},
                            complete: function(res) {},
                        })
                        wx.setStorage({
                            key: 'user_card',
                            data: {
                                user_card: 0
                            },
                        })
                        wx.setStorage({
                            key: 'card',
                            data: {
                                _card: user_num
                            },
                        })
                        wx.setStorage({
                            key: 'user_num',
                            data: {
                                user_num: user_num
                            },
                        })
                    }
                    if(res.code==1){
                        wx.showToast({
                            title: '登录失败',
                            icon: 'loading',
                            duration: 1500,
                            mask: true,
                        })
                    }


                }
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    // 獲取手機驗證碼
    have_yzm: function() {
        var that = this;
        var phone = that.data.user_tel;
        var code = "";
        for (var i = 0; i < 4; i++) {
            code += Math.floor(Math.random() * 10)
        };
        console.log(code + "========");
        that.setData({
            code: code
        })
        wx.request({
            url:api.form.send,
            data: {
                phone: phone,
                code: code
            },
            header: {
                'content-type': "application/x-www-form-urlencoded"
            },
            method: 'POST',
            success: function(res) {
                // 把code存入data判斷
                that.setData({
                    code: code
                })
                wx.showToast({
                    title: '發送成功',
                    icon: 'success',
                    image: '',
                    duration: 1500,
                    mask: true,
                })
            },
            fail: function(res) {},
            complete: function(res) {},
        })
        // 将获取验证码按钮隐藏60s，60s后再次显示
        that.setData({
            is_show: (!that.data.is_show) //false
        })
        settime(that)

    },
    // 用户卡号
    user_num: function(e) { //获取input输入的值
        console.log(e.detail.value)
        var that = this;
        that.setData({
            user_num: e.detail.value
        })
    },
    // 用户姓名
    user_name: function(e) { //获取input输入的值
        console.log(e.detail.value)
        var that = this;
        that.setData({
            user_name: e.detail.value
        })
    },
    // 手机验证码
    user_yzm: function(e) { //获取input输入的值
        console.log(e.detail.value)
        var that = this;
        that.setData({
            user_yzm: e.detail.value
        })
    },
    // 用户手机号
    user_tel: function(e) { //获取input输入的值
        var that = this;
        that.setData({
            user_tel: e.detail.value
        })
        console.log(e.detail.value)
        var user_tel = that.data.user_tel
        if (!(user_tel.length === 11)) {
            wx.showToast({
                title: '请输入正确的手机号',
                duration: 2000
            })
        }
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // 拿去上一次绑卡时的缓存
        wx.getStorage({
            key: 'user_card',
            success: function(res) {
                console.log(res.data.user_card)
                if (res.data.user_card == 0) {
                    wx.redirectTo({
                        url: '/page/health/pages/archives/archives',
                    })
                }
            },
            fail: function(res) {
                console.log(res)
                console.log("nook")
            },
            complete: function(res) {},
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})